package com.china08.yxyapi.api.manage.tec.teaching.continuedu.teatraintask;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLesson;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduTask;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduTaskLesson;
import com.china08.yxyapi.entity.mg.continuedu.YxyContinueduUserTask;
import com.china08.yxyapi.entity.mg.continuedu.YxyContinueduUserTaskLesson;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonItemRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduTaskLessonRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduTaskRepository;
import com.china08.yxyapi.repository.mg.continuedu.YxyContinueduUserTaskLessonItemRepository;
import com.china08.yxyapi.repository.mg.continuedu.YxyContinueduUserTaskLessonRepository;
import com.china08.yxyapi.repository.mg.continuedu.YxyContinueduUserTaskRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 
 * @annotation: 继续教育-师训任务-任务详情
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class YxyTeaTrainTaskDetailController extends YxyApiBaseController {

    @Autowired
    private YxyContinueduTaskRepository yxyContinueduTaskRepository;

    @Autowired
    private YxyContinueduUserTaskRepository yxyContinueduUserTaskRepository;

    @Autowired
    private YxyContinueduLessonItemRepository yxyContinueduLessonItemRepository;

    @Autowired
    private YxyContinueduUserTaskLessonRepository yxyContinueduUserTaskLessonRepository;

    @Autowired
    private YxyContinueduTaskLessonRepository yxyYxyContinueduTaskLessonRepository;

    @Autowired
    private YxyContinueduLessonRepository yxyYxyYxyContinueduLessonRepository;

    @Autowired
    private YxyContinueduUserTaskLessonItemRepository yxyContinueduUserTaskLessonItemRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/continuedu/teatraintask/detail/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String userId = getProfile(request, response).getId();
        Pageable pageable = reqModel.getPageable(Direction.DESC, "lastModifiedDate");
        Page<YxyContinueduUserTaskLesson> page = null;
        Page<YxyContinueduLesson> pageLesson = null;
        String taskId = reqModel.taskId;
        YxyContinueduTask task = yxyContinueduTaskRepository.findById(taskId);

        YxyContinueduUserTask userTask = yxyContinueduUserTaskRepository.findByUserIdAndTaskId(userId, taskId);

        List<RespModel> list = new ArrayList<YxyTeaTrainTaskDetailController.RespModel>();

        Double taskProgress = 0.0;
        // 获取任务进度
        if (userTask != null) {// 不为空 是已报名的任务 查询任务对应的课程
            taskProgress = userTask.getProgress();

            // 查询任务对应课程
            page = yxyContinueduUserTaskLessonRepository.findByTaskIdAndUserId(taskId, userId, pageable);
            Integer i = 1;
            for (YxyContinueduUserTaskLesson yxyUserTaskLesson : page) {
                RespModel model = new RespModel();
                model.endDate = DateToolUtils.format(task.getEndDate(), DateToolUtils.FORMAT_SHORT_YMD);
                model.score = task.getScore();
                model.taskId = task.getId();
                model.taskName = task.getName();
                Integer counrseCount = yxyContinueduLessonItemRepository
                        .countByLessonId(yxyUserTaskLesson.getLessonId());
                model.counrseCount = counrseCount;
                Integer andStatus = yxyContinueduUserTaskLessonItemRepository
                        .countByTaskIdAndLessonIdAndStatusAndUserId(taskId, yxyUserTaskLesson.getLessonId(), 1, userId);
                model.courseId = yxyUserTaskLesson.getLessonId();
                model.courseName = yxyUserTaskLesson.getLessonName();
                model.taskProgress = taskProgress;
                double f = (float) andStatus / counrseCount;
                BigDecimal b = new BigDecimal(f);
                double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() * 100;
                model.progress = f1;
                model.num = i;
                list.add(model);
                i++;
            }
        } else {
            // 未报名的任务
            List<String> lessonIdStrings = new ArrayList<String>();
            // 获取任务对应课程ID
            List<YxyContinueduTaskLesson> byTaskId = yxyYxyContinueduTaskLessonRepository.findByTaskId(taskId);
            for (YxyContinueduTaskLesson yxyContinueduTaskLesson : byTaskId) {
                lessonIdStrings.add(yxyContinueduTaskLesson.getLessonId());
            }

            // 查询课程
            pageLesson = yxyYxyYxyContinueduLessonRepository.findByIdIn(lessonIdStrings, pageable);
            Integer i = 1;
            for (YxyContinueduLesson yxyContinueduTaskLesson : pageLesson) {
                RespModel model = new RespModel();
                model.endDate = DateToolUtils.format(task.getEndDate(), DateToolUtils.FORMAT_SHORT_YMD);
                model.score = task.getScore();
                model.taskId = task.getId();
                model.taskName = task.getName();
                model.counrseCount = yxyContinueduLessonItemRepository.countByLessonId(yxyContinueduTaskLesson.getId());
                model.courseId = yxyContinueduTaskLesson.getId();
                model.courseName = yxyContinueduTaskLesson.getName();
                model.progress = 0.0;
                model.taskProgress = taskProgress;
                model.num = i;
                list.add(model);
                i++;
            }
        }

        Page<RespModel> pageRes = null;

        if (page != null) {
            pageRes = new PageImpl<RespModel>(list, pageable, page.getTotalElements());
        } else {
            pageRes = new PageImpl<RespModel>(list, pageable, pageLesson.getTotalElements());
        }

        return pageRes;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 课程ID
        public String taskId;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 任务名称
        public String taskName;
        // 任务Id
        public String taskId;
        // 截止时间
        public String endDate;
        // 学分数
        public Integer score;
        // 任务进度
        public Double taskProgress;
        // 课程id
        public String courseId;
        // 课时数
        public Integer counrseCount;
        // 学习进度
        public Double progress;
        // 课程名称
        public String courseName;
        // 序号
        public Integer num;
    }
}
